﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace fileTest
{
    /// <summary>
    /// 删除指定文件夹下的修改日期超过n天的文件
    /// 此功能稍加修改就可用于删除服务器iis日志
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo dir = new DirectoryInfo(@"E:\asp.net\wujiaohua4.0\wujiaohua\Backup");
            FileInfo[] fs = dir.GetFiles();
            
            foreach (var f in fs)
            {
                //DateTime dtCreateTime = f.CreationTime;
                DateTime dtLastModifyTime = f.LastWriteTime;
                int days = (int)(DateTime.Now - dtLastModifyTime).TotalDays;
                Console.WriteLine("fullname={0},LastModifyTime={1},{2} days ago.", f.Name, f.LastWriteTime.ToShortDateString(), days);
                if (days > 170)
                {
                    f.Delete();
                    Console.WriteLine("\tfile {0} has deleted.", f.Name);
                }
            }

            Console.ReadKey();
        }
    }
}
